
package com.compuvest.tld;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.ArrayList;

import com.compuvest.web.Category;
// </editor-fold>

public class ExtMenu extends TagSupport {

    protected int categoryID = 0;
    protected int subcategoryID = 0;
    protected String pageUrl;
    protected ArrayList<Category> cats;
    protected ArrayList<Category> scats;
    protected String style;
    protected StringBuilder menu;
    protected StringBuilder smenu;

    public void setCategoryID(int categoryID)               { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID)         { this.subcategoryID = subcategoryID; }
    public void setPageUrl(String pageUrl)                  { this.pageUrl = pageUrl; }
    public void setStyle(String style)                      { this.style = style; }
    public void setCategories(ArrayList<Category> cats)     { this.cats = cats; }
    public void setSubcategories(ArrayList<Category> scats) { this.scats = scats; }

// <editor-fold defaultstate="collapsed" desc="doStartTag()">
    @Override
    public int doStartTag() throws JspException {

	if(style == null) style = "md";

	menu = new StringBuilder(4000);
	smenu = new StringBuilder(3000);
        try {
	    menu.append("<table>");
	    menu.append("<tr><td id='menublock'>");
	    menu.append("<ul class='").append(style).append("' id='menu'>");
	    for(Category catMenu : this.cats) {
		if(categoryID != 0 && categoryID == catMenu.getCategoryID()) {

		    smenu.append("<div style='margin:4px 0;'><div class='menutitle'>").append(catMenu.getName()).append("</div>");
		    smenu.append("<div id='upmenu'>");
		    for(Category scatMenu : this.scats) {
			smenu.append("<div").append(subcategoryID == scatMenu.getCategoryID() ? " class='place'" : "");
			smenu.append("><a href='").append(pageUrl).append("&cid=").append(catMenu.getCategoryID());
			smenu.append("&sid=").append(scatMenu.getCategoryID()).append("'>&nbsp;&#x95;&nbsp;");
			smenu.append(scatMenu.getName()).append("(").append(scatMenu.getItemsInCategory()).append(")</a>");
			smenu.append("</div>");
		    }
		    smenu.append("</div></div>\n");
		}
		menu.append("<li class='").append(style).append("' /><a").append(categoryID == catMenu.getCategoryID() ? " style='background:#ffffbf;'" : "");
		menu.append(" href='").append(pageUrl).append("&cid=").append(catMenu.getCategoryID()).append("'>");
		menu.append(catMenu.getName()).append("(").append(catMenu.getItemsInCategory()).append(")</a>");
	    }
	    menu.append("</ul>");
    	    menu.append("</td></tr></table>");
    	    menu.append("<div style='line-height:2px;'></div>");

	    pageContext.getOut().print("<br/>" + smenu.append(menu).toString());
//            pageContext.getOut().print(smenu.toString() + menu.toString() +"<br/>["+cats.size()+":"+scats.size()+"] "+menu.length()+":"+smenu.length()+" c="+style);
        }
        catch(java.io.IOException e) {
            throw new JspTagException("ExtMenu: " + e.getMessage());
        }
        return SKIP_BODY;
    }
// </editor-fold>

 // <editor-fold defaultstate="collapsed" desc="doEndTag()">
   @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="release()">
    @Override
    public void release() {
	pageUrl = null;
	cats = null;
	scats = null;
	style = null;
	menu = null;
	smenu = null;
    }
// </editor-fold>
}
